﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SN.Models;
using SN.Data;
namespace SN.Report
{
    public partial class FormClass : Form
    {
        public FormClass()
        {
            InitializeComponent();
        }

        private void FormClass_Load(object sender, EventArgs e)
        {
            LoadClassTree();
        }

        private void LoadClassTree()
        {
            List<r_ReportClass> ListClass = (List<r_ReportClass>)DBUtility.DB.From<r_ReportClass>().ToList();
            treeView1.Nodes.Clear();
            foreach (r_ReportClass item in ListClass)
            {
                TreeNode node = new TreeNode();
                node.Text = item.Class_Name;
                node.Tag = item.Class_Id;
                node.SelectedImageIndex = 1;
                node.ImageIndex = 1;
                treeView1.Nodes.Add(node);
            }

        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            txtClassId.Text = "";
            txtClassName.Text = "";

        }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            string ClassId = treeView1.SelectedNode.Tag.ToString();
            r_ReportClass obj = DBUtility.DB.From<r_ReportClass>().Where(r_ReportClass._.Class_Id == ClassId).ToSingle();
            if (obj != null)
            {

                txtClassId.Text = obj.Class_Id.ToString();
                txtClassName.Text = obj.Class_Name;
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtClassId.Text))
            {
                MessageBox.Show("报表类别不能为空,删除失败!", "提示!", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            //开始删除 
            //判断是否已经被使用 
            int iCnt = DBUtility.DB.Count<r_Report>(r_Report._.Report_ClassId == int.Parse(txtClassId.Text));
            if (iCnt > 0)
            {
                MessageBox.Show("该类别已经被使用，不允许删除!", "提示!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            else
            {
                if (DBUtility.DB.Delete<r_ReportClass>(r_ReportClass._.Class_Id == int.Parse(txtClassId.Text)) == 1)
                {
                    treeView1.SelectedNode.Remove();
                    MessageBox.Show("删除成功!", "提示!", MessageBoxButtons.OK);
                    return;
                }
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtClassName.Text))
            {
                MessageBox.Show("分类名称不能为空!", "提示!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            r_ReportClass obj;
            if (string.IsNullOrEmpty(txtClassId.Text))
            {
                //新增的情况下
                obj = new r_ReportClass();
                obj.Class_Name = txtClassName.Text.Trim();

                obj.Detach();

                if (DBUtility.DB.Save<r_ReportClass>(obj) == 1)
                {
                    txtClassId.Text = obj.Class_Id.ToString(); //重新赋值 防止重复增加 
                }
                treeView1.Nodes.Add(new TreeNode() { Tag = obj.Class_Id, Text = obj.Class_Name });
            }
            else
            {
                //修改的情况下
                DBUtility.DB.Update<r_ReportClass>(r_ReportClass._.Class_Name, txtClassName.Text, r_ReportClass._.Class_Id == txtClassId.Text);
            }

            //重新加载
            LoadClassTree();
        }
    }
}
